using System;
using System.IO;
using System.Xml;

namespace Elev.Core.Utility
{
    public class ConfigManager
    {
        const string CONFIG_VERSION = "1.0";

        string appName;
        string configFilePath;
        XmlDocument xmlConfig;

        public string ConfigurationFilePath
        {
            get { return configFilePath; }
        }

        public ConfigManager(string AppName)
        {
            appName = AppName;
            configFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);            
            configFilePath += string.Format(@"{0}{1}{0}{1}.cfg", Path.DirectorySeparatorChar, appName);

            xmlConfig = new XmlDocument();
            if (File.Exists(configFilePath))
            {
                // Load the saved configuration file
                xmlConfig.Load(configFilePath);
            }
            else
            {
                // Create a new xmlConfig 
                XmlNode root = XmlHelper.CreateElement(xmlConfig, "Configuration");
                XmlHelper.AppendAttribute(root, "Version", CONFIG_VERSION);
            }
        }

        public string GetValue(string Key)
        {
            lock (this)
            {
                string xPath = string.Format("//app[@Key = '{0}']", Key);
                XmlNode node = xmlConfig.DocumentElement.SelectSingleNode(xPath);
                
                if (node == null)
                    return "";

                return node.Attributes["Value"].InnerText;
            }
        }

        public string[] GetValues(string Key)
        {
            lock (this)
            {
                string xPath = string.Format("//app[@Key = '{0}']", Key);
                XmlNode node = xmlConfig.DocumentElement.SelectSingleNode(xPath);

                if (node == null)
                    return null;

                XmlNodeList nodes = node.SelectNodes("value");
                string[] s = new string[nodes.Count];

                for (int i = 0; i < nodes.Count; i++)
                    s[i] = nodes[i].InnerText;

                return s;
            }
        }

        public void SetValue(string Key, string Value)
        {
            lock (this)
            {
                string xPath = string.Format("//app[@Key = '{0}']", Key);
                XmlNode node = xmlConfig.DocumentElement.SelectSingleNode(xPath);

                if (node == null)
                {
                    node = XmlHelper.CreateElement(xmlConfig.DocumentElement, "app");
                    XmlHelper.AppendAttribute(node, "Key", Key);
                    XmlHelper.AppendAttribute(node, "Value", "");
                }

                node.Attributes["Value"].InnerText = Value;
            }
        }

        public void SetValues(string Key, string[] Values)
        {
            lock (this)
            {
                string xPath = string.Format("//app[@Key = '{0}']", Key);
                XmlNode node = xmlConfig.DocumentElement.SelectSingleNode(xPath);

                if (node != null)
                    node.ParentNode.RemoveChild(node);

                node = XmlHelper.CreateElement(xmlConfig.DocumentElement, "app");
                XmlHelper.AppendAttribute(node, "Key", Key);

                foreach (string s in Values)
                {
                    XmlHelper.CreateElement(node, "value", s);
                }
            }
        }

        public void Save()
        {
            FileInfo f = new FileInfo(configFilePath);
            if (!Directory.Exists(f.DirectoryName))
                Directory.CreateDirectory(f.DirectoryName);

            XmlTextWriter writer = new XmlTextWriter(configFilePath, null);
            writer.Formatting = Formatting.Indented;
            xmlConfig.PreserveWhitespace = true;
            xmlConfig.Save(writer);
        }
    }
}